$(function(argument) {
	// 时间戳转换成日期(template)
	function new_date(a) {
		var a = arguments[0];
		var date = new Date(Number(a) * 1000);
		var Y = date.getFullYear() + '-';
		M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
		h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
		// var time_new_xu = Y + M + D + h + m + s;
		var time_new_xu = Y + M + D;
		return time_new_xu;
	}
	// 积分页面渲染
	var uid = window.localStorage.getItem("user_id");
	getAllRaffle(uid);
	getRaffle(uid);
	getPrize(uid);
	// 积分总数
	function getAllRaffle(uid) {
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/Order/CountPoint',
			type: 'POST',
			dataType: 'json',
			data: {
				uid: uid
			},
			success: function(data) {
				// console.log(data);
				$(".currentI span").html("当前积分" + data.sing.pay_points);
			}
		})
	}
	// 积分明细
	function getRaffle(uid) {
		$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/Order/PointDetail',
				type: 'POST',
				dataType: 'json',
				data: {
					uid: uid
				},
			})
			.done(function(data) {
				console.log(data);
				if (data.sing.length ==0) {
					$(".tab-paneR").html("<h3 style='line-height: 2rem;' class='ac'>您尚未获得积分</h3>");
				} else {
					var strRaffle = "";
					for (var i = 0; i < data.sing.length; i++) {
						strRaffle +=
							'<div class="getRaffle">' + '<p class="title">商品购买奖励积分</p>' + '<p class="orderNumber">订单号：<span>' + data.sing[i].order_sn + '</span></p>' + '<span class="count">+<span>' + data.sing[i].integral + '</span></span>' + '</div>';
					}
					$(".tab-paneR").html(strRaffle);
				}
			})
			.fail(function() {
				console.log("error");
			})
	}

	function getPrize(uid) {
		$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/AppRandom/RanDetail',
				type: 'POST',
				dataType: 'json',
				data: {
					uid: uid
				},
			})
			.done(function(data) {
				// console.log(data);
				if (data.code == 500) {
					$(".tab-paneP").html("<h3>您还没有抵用券</h3>");
				}
				if (data.code == 200) {
					var strPrize = '';
					for (var i = 0; i < data.sing.length; i++) {
						strPrize += '<div class="voucher">' + '<p><i>' + data.sing[i].random_money + '</i>元抵用券</p>' + '<span>全场通用</span>' + '<p class="validTime">有效期：<em>' + new_date(data.sing[i].end_time) + '</em></p>' + '</div>';
					}
					$(".tab-paneP").html(strPrize);
				}
			})
			.fail(function() {
				console.log("error");
			})
	}
	// 点击奖品
	$(".navP a").click(function() {
			getPrize(uid);
		})
		// 点击积分明细
	$(".navR a").click(function() {
			getRaffle(uid);
		})
		// 点击抽奖
	$(".raffleBtn").click(function() {
		$(".areYouSure").css({
			display: 'block'
		});
	})
	$(".sureS").click(function() {
		// 基础样式更改
		$(".areYouSure").css({
			display: 'none'
		});
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/AppRandom/Random',
			type: 'POST',
			dataType: 'json',
			data: {
				uid: uid
			},
			success: function(data) {
				if (data.code == 500) {
					$(".modal").css({
						display: 'none'
					});
					$(".modal-dialog").css({
						display: 'none'
					});
					$(".modal-backdrop").css({
						display: 'none'
					});
					$(".not").css({
						display: 'block'
					})
					setTimeout(function() {
						$(".not").css({
						display: 'none'
					})
					}, 1500)
				}
				if (data.code == 200) {
					$(".modal-content img").attr('src', '../image/prizeBgi/' + data.sing.money + '.jpg');
				}
				// 重新渲染积分总数
				getAllRaffle(uid);
			}
		})

	})
	$(".modal-content img").click(function() {
			$(".nav-tabs li a").removeClass('active');
			$(".navP a").addClass('active');
			$(".tab-pane").removeClass('active');
			$(".tab-paneP").addClass('active');
			getPrize(uid);
		})
		// 点击取消
	$(".cancel").click(function() {
		$(".areYouSure").css({
			display: 'none'
		});
	})
})